package cn.com.git.admin.config.shiro;

import cn.com.git.admin.common.UpmsConstant;
import cn.com.git.admin.common.UpmsResultConstant;
import cn.com.git.admin.server.api.feign.upms.SystemFeignService;
import cn.com.git.admin.server.api.feign.upms.UpmsFeignService;
import cn.com.git.admin.service.dto.AuthenticationDTO;
import cn.com.git.admin.service.dto.AuthenticationTokenDTO;
import cn.com.git.admin.service.dto.SysResourceListDTO;
import cn.com.git.admin.service.query.SysResourceListQuery;
import cn.com.git.common.model.ResultMsg;
import cn.com.git.common.model.ResultPage;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.util.Arrays;
import java.util.List;

/**
 */
@SuppressWarnings("ALL")
public class ShiroRealm extends AuthorizingRealm {

    @Autowired
    @Lazy
    private UpmsFeignService upmsFeignService;
    @Autowired
    @Lazy
    private SystemFeignService systemFeignService;

    /**
     * 授权
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        //用户账户
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        AuthenticationDTO authenticationDTO = principals.getPrimaryPrincipal()==null?null:(AuthenticationDTO)principals.getPrimaryPrincipal();
        if(authenticationDTO!=null){
            List<SysResourceListDTO> list = null ;
            SysResourceListQuery query = new SysResourceListQuery();
            if(!UpmsConstant.SUPER_ADMIN_ACCOUNT.equals(authenticationDTO.getSysUser().getAccount())){
                query.setIds(authenticationDTO.getResourceIdList());
            }
            ResultPage<SysResourceListDTO> resultPage = systemFeignService.sysResourceListPage(query);
            list = resultPage.getRows();

            if(CollectionUtils.isNotEmpty(list)){
                list.forEach(s->{
                    //查询权限标识
                    authorizationInfo.addStringPermissions(Arrays.asList(s.getM().getPermission()));
                });
            }
            //查询角色标识
            authorizationInfo.addRole("sys_admin");
        }

        //TO-DO 查询权限
        return authorizationInfo;
    }

    /**
     * 用户登录及认证
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        SimpleAuthenticationInfo authenticationInfo = null;
        String account = (String) token.getPrincipal();
        String password = new String((char[]) token.getCredentials());
        AuthenticationDTO authenticationDTO = null;
        ResultMsg<AuthenticationDTO> resultMsg = upmsFeignService.ssoLoginByPassword(account,password);
        if(resultMsg.isSuccess()){
            authenticationDTO = resultMsg.getData();
        }else{
            if(UpmsResultConstant.EMPTY_USERNAME.getCode()==resultMsg.getCode() || UpmsResultConstant.INVALID_ACCOUNT.getCode()==resultMsg.getCode()){
                throw new UnknownAccountException();
            }else if(UpmsResultConstant.EMPTY_PASSWORD.getCode()==resultMsg.getCode()||UpmsResultConstant.INVALID_PASSWORD.getCode()==resultMsg.getCode() ){
                throw new IncorrectCredentialsException();
            }
        }
        authenticationInfo = new SimpleAuthenticationInfo(authenticationDTO,password, getName());
        return authenticationInfo;

    }
}
